const app = getApp()
import api from '../../../config/api'
import path from '../../../config/path'
const { $Message } = require('../../../dist/base/index');
Page({
  data: {
    recordId:'',
    showModal: false,
    qrCodeUrl:'../../../images/pay.png',
    pageData:{
      pageNo:1,
      pageSize:10,
      count:0,
    },
    rentRecords: [
    ],
  },
  showPaymentModal: function(event) {
    const recordId = event.currentTarget.dataset.id;
    
    // 根据 recordId 获取二维码链接
    this.setData({
      // qrCodeUrl: this.getQRCodeUrl(recordId), 
      // 生成二维码的 URL
      showModal: true,
      recordId:recordId,
    });
  },
  closeModal: function() {
    this.setData({
      showModal: false
    });
  },
  handlePayment(event) {
    api.post('wechat/successPay/'+this.data.recordId).then(res=>{
      if (res.code === '200') {
        this.setData({
          showModal: false
        });
        this.selectList()
      }
    }).catch(err=>{
      console.log(err)
    })
    // 修改状态
    wx.showToast({ title: '支付成功', icon: 'success' });
    // wx.showModal({
    //   title: '确认支付',
    //   content: '您确认要支付此租金记录吗？',
    //   success: (res) => {
    //     if (res.confirm) {
    //       // 执行支付逻辑...

    //       // 更新状态或界面
    //     }
    //   },
    // });
  },

   /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //向后端发送请求，查询消息列表
    this.selectList();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.selectList();
  },
  selectList(){
    const currentDate = new Date();
    let params = {
      pageNo:this.data.pageData.pageNo,
      pageSize:this.data.pageData.pageSize
    }
    api.post('wechat/RentingFindPage',params).then(res=>{
      this.data.pageData.count = res.data.count;

      // 更新状态
      res.data.list.forEach(record => {
      const payTime = new Date(record.payTime);
      const payableTime = new Date(record.payableTime);

      if (payTime > currentDate) {
        record.state = '未到时间'; // 0: 未到时间
      } else if (payableTime < currentDate && record.stateStr == '已交费') {
        record.state = '已交费'; // 2: 已交租
      } else {
        record.state = '已到时间'; // 1: 已到时间
      }
    });
      this.setData({
        rentRecords:res.data.list,
        count:res.data.count
      })
    }).catch(err=>{
      console.log(err)
    })
  }
});